'use strict';
/**
 * @name: 本地mongodb连接
 * @description: 本地测试的mongodb连接
 * @version: 0.0.1
 * @author: xiech
 * @Date: 2020-12-09 10:47:13
 * @LastEditors: xiech
 * @LastEditTime: 2020-12-09 10:47:14
 */

const MongoClient = require('mongodb').MongoClient;
const db_url = 'mongodb://192.168.0.233:27017';

module.exports = async () => {
  let time = new Date().getTime();
  return new Promise((resolve, reject) => {
    try {
      MongoClient.connect(db_url, { useNewUrlParser: true, useUnifiedTopology: true,  connectTimeoutMS: 5000  } , async (err, db) => {
        // console.log('连接mongodb时长:::',new Date().getTime() - time);
        if (!err) {
          const result = {
            db: db,
            mongodb: db.db('mfmongodb')
          };
          resolve(result);
        } else {
          console.log('----------------------');
          console.log('连接 mongodb 失败,请查看!关于 mongodb 的连接地址，请个人连接自己局域网中的库,如果没有请自行搭建,或者寻找网管!', JSON.stringify(err));
          console.log('----------------------');
          resolve({});
        }
      });
    } catch (err) {
      reject('连接 mongodb 失败,请查看!关于 mongodb 的连接地址，请个人连接自己局域网中的库,如果没有请自行搭建,或者寻找网管!', JSON.stringify(err));
    }

  });
};